package cn.yuxiaoyuan.controller;

import cn.yuxiaoyuan.common.authorize.PreAuthorize;
import cn.yuxiaoyuan.common.util.R;
import cn.yuxiaoyuan.service.IUserService;
import cn.yuxiaoyuan.user.domain.User;
import cn.yuxiaoyuan.user.query.UserQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author solargen
 * @version 1.0
 * @date 2025/5/11 10:37
 */
@Api(value = "user相关的接口")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @PreAuthorize(sn = "user:findById",name = "根据id查询用户",descs = "根据id查询用户")
    @GetMapping("/{id}")
    public R findById(@PathVariable("id")Long id){
        return R.ok(userService.findById(id));
    }

    @ApiOperation(value = "查询全部用户信息")
    @GetMapping
    @PreAuthorize(sn = "user:findAll",name = "查询全部用户",descs = "查询全部用户")
    public R findAll(){
        return R.ok(userService.findAll());
    }

    @DeleteMapping("/{id}")
    @PreAuthorize(sn = "user:deleteById",name = "根据id删除用户",descs = "根据id删除用户")
    public R deleteById(@PathVariable("id")Long id){
        userService.deleteById(id);
        return R.ok();
    }
    @PostMapping("/deleteAll")
    @PreAuthorize(sn = "user:batchDelete",name = "根据ids批量删除用户",descs = "根据ids批量删除用户")
    public R batchDelete(@RequestBody List<Long> ids){
        userService.batchDelete(ids);
        return R.ok();
    }

    @PostMapping
    @PreAuthorize(sn = "user:add",name = "添加或修改用户",descs = "添加或修改用户")
    public R add(@RequestBody User user){
        if(user.getId() == null){
            userService.add(user);
        }else {
            userService.update(user);
        }
        return R.ok();
    }

    @PostMapping("/list")
    @PreAuthorize(sn = "user:pageList",name = "用户的分页查询",descs = "用户的分页查询")
    public R pageList(@RequestBody UserQuery userQuery){
        return userService.pageList(userQuery);
    }
}
